package fxLib
{
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	
	import flash.display.MovieClip;
	
	public class FXFadeSlide
	{
		private static const nPROMPTER_FLASH_DURATION:Number  = 1500 / 1000;
		private static const nPROMPTER_FADEIN_PORTION:Number  = 0.1;
		private static const nSLIDE_DISTANCE:Number           = 40;
		private static const nSLIDE_TO_YPOS:Number            = -60.50;
		private static const nSLIDE_DURATION:Number           = 0.5;
		
		// set defaults for gettersetters
		private var mnSlideDistance:Number                    = nSLIDE_DISTANCE;
		private var mnSlideToYPos:Number                      = nSLIDE_TO_YPOS;
		private var mnSlideDuration:Number                    = nSLIDE_DURATION;
		
		private var mcSlideUpTween:Tween;
		private var mcFadeUpTween:Tween;
		
		private var mbSlideDone:Boolean;
		private var mbFadeUpDone:Boolean;
		
		// constructor
		// call this function to make an object slide up while fading in + glow-flash
		public function FXFadeSlide( acMCtoSlide:MovieClip, acParams:Object = null )
		{
			// accept parameters if given, in Object format
			if (acParams != null)
			{
				if ( !isNaN( acParams.slideToYPos ) )
					mnSlideToYPos       = acParams.slideToYPos;
				if ( !isNaN( acParams.slideDistance ) )
					mnSlideDistance     = acParams.slideDistance;
				if ( !isNaN( acParams.slideDuration ) )
					mnSlideDuration     = acParams.slideDuration;
			}
			
			mbSlideDone                 = false;
			mbFadeUpDone                = false;
			
			//trace("[FXSlide constructor] acMCtoSlide:"+acMCtoSlide.name+" initialY:"+(mnSlideToYPos + mnSlideDistance)+"  finalY:"+mnSlideToYPos);
			
			mcSlideUpTween              = new Tween( acMCtoSlide, "y", Strong.easeOut, (mnSlideToYPos + mnSlideDistance), mnSlideToYPos, mnSlideDuration, true );
			mcFadeUpTween               = new Tween( acMCtoSlide, "alpha", None.easeNone, 0.0, 1.0, mnSlideDuration, true );
			
			mcSlideUpTween.addEventListener( TweenEvent.MOTION_FINISH, slideFinished);
			mcFadeUpTween.addEventListener( TweenEvent.MOTION_FINISH, fadeUpFinished);
		}
		
		public function destroy():void
		{
			mcSlideUpTween              = null;
			mcFadeUpTween               = null;
			
			//trace("[FXSlide destroy]");
		}
		
		public function stop():void
		{
			if (!mbSlideDone)
			{
				mcSlideUpTween.stop();
				mcSlideUpTween.removeEventListener( TweenEvent.MOTION_FINISH, slideFinished, false );
			}
			else
			{
				//trace("[FXSlide stop] UNEXPECTED STOP -- SlideDone, huh?");
			}
			
			if (!mbFadeUpDone)
			{
				mcFadeUpTween.stop();
				mcFadeUpTween.removeEventListener( TweenEvent.MOTION_FINISH, fadeUpFinished, false );
			}
			else
			{
				//trace("[FXSlide stop] UNEXPECTED STOP -- FadeUpDone, huh?");
			}
			
		}
		
		private function slideFinished( e:TweenEvent ):void
		{
			mbSlideDone                 = true;
			mcSlideUpTween.removeEventListener( TweenEvent.MOTION_FINISH, slideFinished );
			
			// only destroy once both tweens have finished.
			if (mbSlideDone && mbFadeUpDone)
			{
				destroy();
			}
		}
		
		private function fadeUpFinished( e:TweenEvent ):void
		{
			mbFadeUpDone                = true;
			mcFadeUpTween.removeEventListener( TweenEvent.MOTION_FINISH, fadeUpFinished );
			
			// only destroy once both tweens have finished.
			if (mbSlideDone && mbFadeUpDone)
			{
				destroy();
			}
		}
		
		//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		
		public function set slideDistance( anSlideDistance:Number ):void
		{
			mnSlideDistance = anSlideDistance;
		}
		
		public function get slideDistance():Number
		{
			return mnSlideDistance;
		}
		
		public function set slideToYPos( anSlideToYPos:Number ):void
		{
			mnSlideToYPos = anSlideToYPos;
		}
		
		public function get slideToYPos():Number
		{
			return mnSlideToYPos;
		}
		
		public function set slideDuration( anSlideDuration:Number ):void
		{
			mnSlideDuration = anSlideDuration;
		}
		
		public function get slideDuration():Number
		{
			return mnSlideDuration;
		}
		
		//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		
	}
}